package com.zzptc.easyshop.db;

import com.zzptc.easyshop.util.MyUtil;
import com.zzptc.easyshop.vo.Users;
import com.zzptc.easyshop.vo.VO;

import java.sql.*;
import java.util.Map;

public class MysqlDB implements IDB {
    //全局变量
    private Connection conn;
    private Map map;
    private String DRIVER="com.mysql.jdbc.Driver";
    private String URL="jdbc:mysql://localhost:3306/easybuy?useUnicode=true&characterEncoding=utf8";
    private String UNAME="root";
    private String PWD="000000";
//    private String DRIVER;
//    private String URL;
//    private String UNAME;
//    private String PASS;
//      private String CHARACTER;
    @Override
    public Connection getConn() throws SQLException {
        try {
//            map = MyUtil.readXML("db-config.xml");//用自己写的工具类读取配置文件信息
//            DRIVER = (String) map.get("driver");
//            URL = (String) map.get("url");
//            UNAME = (String) map.get("uname");
//            PASS = (String) map.get("pwd");
//            CHARACTER=(String) map.get("character");
            Class.forName(DRIVER);
        } catch (Exception e) {
            e.printStackTrace();
        }
        //获取连接   URL数据库连接符
        conn = DriverManager.getConnection(URL, UNAME, PWD);
        return conn;
    }
//        try {
//            conn = DriverManager.getConnection(URL+"&"+CHARACTER, UNAME, PASS);
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        }

    @Override
    public void closeAll(Connection conn, PreparedStatement ps, ResultSet rs) throws SQLException {
        if (conn!=null&&!conn.isClosed()) {
            conn.close();
        }
        if (ps!=null) {
            ps.close();
        }
        if (rs!=null) {
            rs.close();
        }
    }

    @Override
    public void closeConn() {

    }


    public static void main(String[] args) {
        IDB db = new MysqlDB();

        try {
            System.out.println(db.getConn().isClosed());
        } catch (SQLException e) {
            e.printStackTrace();
        }


    }
}
